package com.zhuannimei.ui;

import org.json.JSONObject;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.R;
import com.zhuannimei.dialog.BaseDialog;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.utility.MessageHandlerList;
import com.zhuannimei.utility.MessageTypes;
import com.zhuannimei.utility.MessageUtil;
import com.zhuannimei.utility.PersistTool;
import com.zhuannimei.utility.UiUtil;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.view.BubbleView;
import com.zhuannimei.view.XXButton;
import com.zhuannimei.view.XXRelativeLayout;

public class TabFourActivity extends BaseActivity implements OnClickListener {

	private XXRelativeLayout rl_setting_mypoint;
	private XXRelativeLayout rl_setting_buyhistory;
	private XXRelativeLayout rl_setting_beadd;
	private XXRelativeLayout rl_setting_pointlist;
	private XXRelativeLayout rl_setting_newslist;
	// private XXRelativeLayout rl_setting_checkversion;
	// private XXRelativeLayout rl_duihuan_history;
	private TextView tvName;
	private TextView tvPoint;
	private ImageView message;
	private XXButton exit;
	private BaseDialog exitDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.setting);
		exitDialog = new BaseDialog(TabFourActivity.this);
		exitDialog.setCancelable(true);
		exitDialog.setCanceledOnTouchOutside(true);
		exitDialog.setTitle("退出");
		exitDialog.setMessage("您确定退出该应用吗?");

		initView();
		setListener();
		getMypoint(ZnmData.getUserData().getUserUserId());
	}

	@Override
	protected void handleMsg(Message msg) {
		super.handleMsg(msg);
		switch (msg.what) {
		case MessageTypes.UPDATE_MESSAGE_NOTICE:
			int num = PersistTool.getInt(MessageUtil.MESSAGE_UNREADNUM);
			if (num != 0) {
				message.setVisibility(View.VISIBLE);
			} else {
				message.setVisibility(View.GONE);
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("设置");
		tvPoint.setText(ZnmData.getUserData().getUserPoint() + "妹币");
		int num = PersistTool.getInt(MessageUtil.MESSAGE_UNREADNUM);
		WuLog.i("wulijie", "" + num);
		if (num != 0) {
			message.setVisibility(View.VISIBLE);
		} else {
			WuLog.i("wulijie", "隐藏了" + num);
			message.setVisibility(View.GONE);
		}
	}

	private void setListener() {
		rl_setting_mypoint.setOnClickListener(this);
		rl_setting_buyhistory.setOnClickListener(this);
		rl_setting_beadd.setOnClickListener(this);
		rl_setting_pointlist.setOnClickListener(this);
		rl_setting_newslist.setOnClickListener(this);
		exit.setOnClickListener(this);
		// rl_setting_checkversion.setOnClickListener(this);
		// rl_duihuan_history.setOnClickListener(this);
	}

	private void initView() {
		rl_setting_mypoint = (XXRelativeLayout) findViewById(R.id.rl_setting_mypoint);
		rl_setting_buyhistory = (XXRelativeLayout) findViewById(R.id.rl_setting_buyhistory);
		rl_setting_beadd = (XXRelativeLayout) findViewById(R.id.rl_setting_beadd);
		rl_setting_pointlist = (XXRelativeLayout) findViewById(R.id.rl_setting_pointlist);
		rl_setting_newslist = (XXRelativeLayout) findViewById(R.id.rl_setting_newslist);
		exit = (XXButton) findViewById(R.id.setting_rl_exit);
		tvName = (TextView) findViewById(R.id.setting_name_tv);
		tvName.setText("" + ZnmData.getUserData().getUserName());
		tvPoint = (TextView) findViewById(R.id.setting_tv_point);
		tvPoint.setText("" + ZnmData.getUserData().getUserPoint());
		message = (ImageView) findViewById(R.id.iv_new_youhui_iv4);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.rl_setting_mypoint:
			// 我的积分
			// getMypoint(ZnmData.getUserData().getUserUserId());
			break;
		case R.id.rl_setting_buyhistory:
			intent = new Intent(TabFourActivity.this, BuyHistoryActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_setting_beadd:
			// 成为推广员
			intent = new Intent(TabFourActivity.this, JoinUsActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_setting_pointlist:
			// 积分榜
			intent = new Intent(TabFourActivity.this, TabThreeActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_setting_newslist:
			// 消息中心
			intent = new Intent(this, MessageActivity.class);
			PersistTool.saveInt(MessageUtil.MESSAGE_UNREADNUM, 0);
			startActivity(intent);
			break;
		case R.id.setting_rl_exit:
			// 退出登录
			exitDialog.setButton1("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					ZnmData.getUserData().clear();
					MessageHandlerList.sendMessage(BaseActivity.UI_FINISH);
					exitDialog.dismiss();
				}
			});
			exitDialog.setButton2("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					exitDialog.cancel();
				}
			});
			exitDialog.show();
			break;
		default:
			break;
		}
	}

	/**
	 * 获取我的积分
	 */
	private void getMypoint(String userId) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("userId", userId);
		RequestServerData.getMypoint(params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				String result = responseInfo.result;

				try {
					JSONObject jobj = new JSONObject(result);
					String code = jobj.optString("code");
					String desc = jobj.optString("desc");
					if ("0".equals(code)) {
						String jsonArray = jobj.optString("result");
						// Toast.makeText(TabFourActivity.this, jsonArray,
						// 0).show();
						if (!TextUtils.isEmpty(jsonArray)) {
							tvPoint.setText("" + jsonArray + "妹币");
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				UiUtil.showToast("error<<" + msg);
				tvPoint.setText("" + ZnmData.getUserData().getUserPoint()
						+ "妹币");
			}

			@Override
			public void onStart() {
				super.onStart();
			}
		});

	}
}
